package com.bvr;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Vector;

import com.bvr.CalendarDayMarker;
import com.bvr.CalendarView;
import com.bvr.CalendarView.OnMonthChangedListener;
import com.bvr.CalendarView.OnSelectedDayChangedListener;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class PropertyReservedCalenderActivity extends Activity {
	private CalendarView _calendar;
	private Button btnGoBack;
	private TextView lblPropertyTitle;
	private static String strPropertyReservedDates;
	CalendarDayMarker[] calendarDayMarker;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.property_reserved_calender);
		setUI();
		_calendar = (CalendarView) findViewById(R.id.calendar_view);
		_calendar.setOnMonthChangedListener(new OnMonthChangedListener() {
			public void onMonthChanged(CalendarView view) {
				markDays();
			}
		});

		_calendar.setOnSelectedDayChangedListener(new OnSelectedDayChangedListener() {
					public void onSelectedDayChanged(CalendarView view) {
						View[] vs = new View[2];

						TextView tv1 = new TextView(
								PropertyReservedCalenderActivity.this);
						tv1.setText("TODO:");
						vs[0] = tv1;

						TextView tv2 = new TextView(
								PropertyReservedCalenderActivity.this);
						tv2.setText("Put events for this day here.");
						vs[1] = tv2;

						view.setListViewItems(vs);
					}
				});
		markDays();
	}

	private void markDays() {
		// TODO: Find items in the range of _calendar.getVisibleStartDate() and
		// _calendar.getVisibleEndDate().
		// TODO: Create CalendarDayMarker for each item found.
		// TODO: Pass CalendarDayMarker array to
		// _calendar.setDaysWithEvents(markers)

		// Example of setting just today with an event
		// _calendar.setDaysWithEvents(new CalendarDayMarker[] { new
		// CalendarDayMarker(Calendar.getInstance(), Color.RED) });

		
		String finalString = "";

		Bundle recdData = getIntent().getExtras();
		strPropertyReservedDates = recdData
				.getString("com.bvr.propertyReservedDates");
		int imonth, iday, iyear;

		int strLen = strPropertyReservedDates.length() - 1;
		char lastchar = strPropertyReservedDates.charAt(strLen);

		if (lastchar == '|') {
			finalString = strPropertyReservedDates.substring(0, strLen - 1);
		} else {
			finalString = strPropertyReservedDates;
		}

		String[] dates = splitString(finalString, "|");

		CalendarDayMarker[] theMarker = new CalendarDayMarker[dates.length];
		int pointer = 0;
		
		for (String oneDate : dates) {
			String[] dateParts = splitString(oneDate, "/");
			String smonth = dateParts[0].toString();
			String sday = dateParts[1].toString();
			String syear = dateParts[2].toString();
			imonth = Integer.parseInt(smonth.trim());
			iday = Integer.parseInt(sday.trim());
			iyear = Integer.parseInt(syear.trim());
		theMarker[pointer] = new CalendarDayMarker(iyear, imonth-1,
					iday, Color.RED);
		
		pointer ++;		
		}
/*
		CalendarDayMarker[] ms = new CalendarDayMarker[4];

		ms[0] = new CalendarDayMarker(2011, 4, 10, Color.GREEN);
		ms[1] = new CalendarDayMarker(2011, 4, 12, Color.GREEN);
		ms[2] = new CalendarDayMarker(2011, 4, 13, Color.GREEN);
		ms[3] = new CalendarDayMarker(2011, 4, 14, Color.GREEN);
*/
		_calendar.setDaysWithEvents(theMarker);

	}

	private void setUI() {
		
		Bundle recdData = getIntent().getExtras();
		String strPropertyTitle = recdData
				.getString("com.bvr.propertyTitle");

		// calendarDayMarker = new CalendarDayMarker[]{};
		btnGoBack = (Button) findViewById(R.id.btnPropertyDetailGoBackCalender);
		btnGoBack.setOnClickListener(goBackListener);
		
		lblPropertyTitle = (TextView) findViewById(R.id.lblPropertyDetailTitleCal);
		lblPropertyTitle.setText("Reserved Dates of " + strPropertyTitle);
		
	}

	private OnClickListener goBackListener = new OnClickListener() {
		public void onClick(View view) {
			Intent intent = new Intent();
			setResult(RESULT_OK, intent);
			finish();
		}
	};

	private String[] splitString(String original, String separator) {

		Vector nodes = new Vector();

		// Parse nodes into vector
		int index = original.indexOf(separator);

		while (index >= 0) {
			nodes.addElement(original.substring(0, index));

			original = original.substring(index + separator.length());

			index = original.indexOf(separator);

		}
		// Get the last node
		nodes.addElement(original);

		// Create split string array
		String[] result = new String[nodes.size()];

		if (nodes.size() > 0) {

			for (int loop = 0; loop < nodes.size(); loop++)

			{
				result[loop] = (String) nodes.elementAt(loop);

				// System.out.println(result[loop]);

			}

		}
		return result;
	}
}
